const db = require('../db/index')

// 浏览
exports.getActivityList = (req,res)=>{
    const sqlstr = `select * from activityinfo where is_delete=0 and exa_state=1 order by activityId asc`
    db.query(sqlstr,(err,results)=>{
        if(err){
            return res.send({status:1,message:err.message})
        }else{
            res.send({
                status:0,
                message:'获取列表成功',
                data:results
            })
        }
    })
}

 // 增加
exports.addActivity = (req,res)=>{
    const sqlStr = `select * from activityinfo where activityTitle=?`
    db.query(sqlStr,req.body.activityTitle,(err,results)=>{
        if(err) return res.cc(err)
        if(results.length === 1 && results[0].activityTitle == req.body.activityTitle) return res.cc('活动标题被占用')
        const sql =`insert into activityinfo set ?`
        db.query(sql,req.body,(err,results)=>{
            if(err) return res.cc(err)
            if(results.affectedRows !== 1) return res.cc('提交活动申请失败')
            res.cc('提交活动申请成功')
        })
    })
}

// 删除
exports.deleteActivity = (req,res)=>{
    const sqlStr = `update activityinfo set is_delete=1 where activityId=?`
    db.query(sqlStr,req.params.activityId,(err,results)=>{
        if(err) return res.cc(err)
        if(results.affectedRows !== 1) return res.cc('删除活动失败')
        res.cc('删除活动成功',0)
    })
}
// 修改
exports.updateActivityById = (req,res)=>{
    const sqlStr = `select * from activityinfo where activityId<>? and activityTitle=?`
    db.query(sqlStr,[req.body.activityId,req.body.activityTitle,req.body.activityContent],(err,results)=>{
        if(err) return res.cc(err)
        if(results.length === 2) return res.cc('标题和内容都被占用,请更换后重试')
        if(results.length === 1 && results[0].activityTitle == req.body.activityTitle && results[0].activityContent == req.body.activityContent){
            return res.cc('新闻标题和新闻内容都被占用')
        }
        if(results.length === 1 && results[0].activityTitle == req.body.activityTitle) return res.cc('新闻标题被占用')
        if(results.length === 1 && results[0].activityContent == req.body.activityContent) return res.cc('新闻内容被占用')
        const sql = `update activityinfo set ? where activityId =?`
        db.query(sql,[req.body,req.body.activityId],(err,results)=>{
            if(err) return res.cc(err)
            if(results.affectedRows !== 1) return res.cc('更新失败')
            res.cc('更新成功')
        })
    })
}
// 查询
exports.getActivityById = (req,res)=>{
    const sqlStr = 'select * from activityinfo where activityTitle=?'
    db.query(sqlStr,req.params.activityTitle,(err,results)=>{
        if(err) return res.cc(err)
        if(results.length !== 1) return res.cc('获取活动数据失败')
        res.send({
            status:0,
            message:'获取活动数据成功',
            data:results[0]
        })
    })
    
}
// 得到审批页面
exports.getActivityByExa = (req,res)=>{
    const sqlstr = `select * from activityinfo where exa_state=0 order by activityId asc`
    db.query(sqlstr,(err,results)=>{
        if(err){
            return res.send({status:1,message:err.message})
        }else{
            res.send({
                status:0,
                message:'获取列表成功',
                data:results
            })
        }
    })
}
// 活动审批通过
exports.goActivityByExa = (req,res)=>{
    const sqlstr = `update activityinfo set exa_state=1 where activityId=?`
    db.query(sqlstr,req.params.activityId,(err,results)=>{
        if(err) return res.cc(err)
        if(results.affectedRows !== 1) return res.cc('申请失败')
        res.cc('通过申请',0)
    })
}



